package com.onlinevideo.dao.impl;

import com.onlinevideo.dao.BaseDao;
import com.onlinevideo.dao.FavoritesDao;
import com.onlinevideo.entity.Favorites;
import com.onlinevideo.entity.FavoritesVideo;

import java.util.List;

public class FavoritesDaoImpl extends BaseDao implements FavoritesDao {

    @Override
    public int insert(Favorites favorites) {
        String sql ="INSERT INTO `T_favorites` (`user_id`,`favorites_name`) VALUES (?,?)";
        return super.update(sql,favorites.getUserId(),favorites.getFavoritesName());
    }

    @Override
    public int delete(Long id) {
        if(id==0){
            return 0;
        }
        String sql = "DELETE FROM `T_favorites` WHERE `id`=?";
        return super.update(sql,id);
    }

    @Override
    public int update(Favorites favorites) {
        String sql ="UPDATE `T_favorites` SET `user_id`=?,`favorites_name`=? WHERE `id`=?";
        return super.update(sql, favorites.getUserId(),favorites.getFavoritesName(),favorites.getId());
    }

    @Override
    public Favorites queryById(Long id) {
        String sql ="SELECT `id`,`user_id` AS `userId`,`favorites_name` AS `favoritesName` FROM `T_favorites` WHERE 1=1 AND `id`=? ";
        return super.queryForOne(Favorites.class,sql,id);
    }

    @Override
    public Favorites queryByFavoriteName(Favorites favorites){
        String sql ="SELECT `id`,`user_id` AS `userId`,`favorites_name` AS `favoritesName` FROM `T_favorites` WHERE 1=1 AND `user_id` = ?  AND `favorites_name`=? ";
        return super.queryForOne(Favorites.class,sql,favorites.getUserId(),favorites.getFavoritesName());
    }

    @Override
    public Long queryByFavoriteCount(Long userId) {
        String sql = "SELECT count(1) FROM `T_favorites` WHERE `user_id`=? ";
        return (Long) super.queryForSingleValue(sql, userId);
    }

    @Override
    public List<Favorites> queryAll() {
        String sql ="SELECT `id`,`user_id` AS `userId`,`favorites_name` AS `favoritesName` FROM `T_favorites` WHERE 1=1 ";
        return super.queryForList(Favorites.class,sql);
    }

    @Override
    public List<Favorites> queryUser(Long userId) {
        String sql ="SELECT `id`,`user_id` AS `userId`,`favorites_name` AS `favoritesName` FROM `T_favorites` WHERE 1=1 AND `user_id`=?";
        return super.queryForList(Favorites.class,sql,userId);
    }
}
